var db;

function initDatabase() {
    db = LocalStorage.openDatabaseSync("DB", "1.0", "A box who remembers its position", 100000);
    try {
        db.transaction( function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS Top(\
               ID INTEGER PRIMARY KEY AUTOINCREMENT,\
               score           INT NOT NULL\
            );');}
        )
    } catch (err) {
        console.log("Error creating table in database: " + err)
    };
}


function readData() {
    if(!db) { return; }

    db.transaction( function(tx) {
        var result = tx.executeSql('select * from Top order by score DESC limit 0,10;');
        var i = 0
        for(var i=0; i < result.rows.length; i++) {
              topModel.append({number: i + 1, score: result.rows.item(i).score})
        }
    })
}

function insertData(value) {
    var res
    if(!db) { return; }

    db.transaction( function(tx) {
        var result = tx.executeSql('INSERT INTO Top (score) VALUES (?);', [value]);
        if (result.rowsAffected > 0) {
            res = "OK";
        } else {
            res = "Error";
        }
    })
    console.log(res)
}

